package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    //注入redis模板
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 批量起售停售及单个起售停售
     */
    @PostMapping("/status/{type}")
    public R updateStatus(@RequestParam List<Long> ids, @PathVariable Integer type) {
        int rows = dishService.updateStatus(ids, type);
        if (rows > 0) {
            return R.success("修改起售停售状态成功");
        } else {
            return R.error("修改起售停售状态失败");
        }
    }

    /**
     * 单个删除和批量删除菜品
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        int rows = dishService.delete(ids);
        if (rows > 0) {
            return R.success("批量删除成功");
        } else {
            return R.error("批量删除失败");
        }
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        //获取分类id
        Long CategoryId=dishService.findCategoryId(dishDto.getId());
        //合成修改前缓冲分类菜品
        String keyed= "dish_"+CategoryId+"_1";
        //删除对应的分类菜品
        redisTemplate.delete(keyed);
        //再删除修改后的分类菜品
        String keying="dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(keying);

        int rows = dishService.update(dishDto);

        if (rows > 0) {
            return R.success("修改菜品成功");
        } else {
            return R.error("修改菜品失败");
        }
    }


    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto) {
        //System.out.println("添加菜品");
//        int rows = dishService.add(dishDto);
//        if (rows > 0) {
//            return R.success("添加菜品成功");
//        } else {
//            return R.error("添加菜品失败");
//        }


        int rows = dishService.add(dishDto);
        //并入redis
        String key= "dish_"+ dishDto.getCategoryId()+"_1";
        //删除对应的菜品的缓存
        redisTemplate.delete(key);
        if (rows > 0) {
            return R.success("添加菜品成功");
        } else {
            return R.error("添加菜品失败");
        }
    }

    /**
     * 分页查询菜品
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R<IPage<DishDto>> findPage(PageParam pageParam) {
        try {
            IPage page = dishService.findPage(pageParam);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    //用户端查看菜品接口
    @GetMapping("list")
    public R<List<Dish>> dishList(Long categoryId, Integer status) {
//        return R.success(dishService.dishList(categoryId, status));

        //加入redis缓存
        List<Dish> list = null;
        String key = "dish_" + categoryId + "_" + status;
        list = (List<Dish>) redisTemplate.opsForValue().get(key);
        if (list != null) {
            return R.success(list);
        }
        list = dishService.dishList(categoryId, status);
        redisTemplate.opsForValue().set(key, list, 6000, TimeUnit.SECONDS);
        return R.success(list);

    }
}
